﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLayer;
using Library;

public partial class Administrator_NewsEdit : System.Web.UI.Page
{
    string cmd = "";
    int Id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        cmd = Request.QueryString["cmd"].ToString();
        switch (cmd)
        {
            case "new":
                txtTacgia.Text = Library.AppEnvironment.SESSION_USER_NAME;
                txtTacgia.ReadOnly = true;
                rbStatus.Enabled = false;
                rbComment.Enabled = false;
                btnNew.Enabled = true;
                btnUpdate.Enabled = false;
                break;
            case "edit":
                txtTacgia.Text = Library.AppEnvironment.SESSION_USER_NAME;
                txtTacgia.ReadOnly = false;
                rbStatus.Enabled = true;
                rbComment.Enabled = true;
                btnNew.Enabled = false;
                btnUpdate.Enabled = true;
                Id = Convert.ToInt32(Request.QueryString["Id"].ToString());
                if (!IsPostBack)
                {
                    FillData(News.GetOne(Id));
                }
                break;
        }
    }
    protected void BbtnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("NewsManager.aspx");
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        News news = new News();
        news.Title = txtTen.Text;
        news.News_Type_ID = Convert.ToInt32(DropDownList1.SelectedValue);
        news.Writer = txtTacgia.Text;
        news.Contents = txtMota.Text;
        news.Writing_Time = DateTime.Today;
        news.Allow_Comment = Convert.ToBoolean(rbComment.SelectedValue);
        news.News_Status = Convert.ToInt32(rbStatus.SelectedValue);
        try
        {
            News.Insert(news);
            MessageBox.Show("Tin tức đã thêm thành công !");
            Response.Redirect("NewsManager.aspx");
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        News news = new News();
        news.News_ID = Id;
        news.Title = txtTen.Text;
        news.News_Type_ID = Convert.ToInt32(DropDownList1.SelectedValue);
        news.Writer = txtTacgia.Text;
        news.Contents = txtMota.Text;
        news.Writing_Time = DateTime.Today;
        news.Allow_Comment = Convert.ToBoolean(rbComment.SelectedValue);
        news.News_Status = Convert.ToInt32(rbStatus.SelectedValue);
        try
        {
            News.Update(news);
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    protected void FillData(News news)
    {
        DropDownList1.SelectedValue = news.News_Type_ID.ToString();
        txtTen.Text = news.Title;
        txtMota.Text = news.Contents;
        rbStatus.Items.FindByValue(news.News_Status.ToString()).Selected = true;
        rbComment.Items.FindByValue(news.Allow_Comment.ToString()).Selected = true;
    }
}